/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.uniscol.dao.impl;

import fr.uniscol.dao.IAdminBasesDao;
import fr.uniscol.dao.UniscolDaoSupport;
import fr.uniscol.model.BaseAnneeScolaire;
import java.util.List;
import org.springframework.stereotype.Repository;

import static fr.uniscol.dao.impl.SQLNames.*;
import fr.uniscol.dao.mapper.BaseAnneeScolaireRowMapper;

/**
 *
 * @author Christophe
 */
@Repository("adminBasesDao")
public class AdminBasesDaoImpl extends UniscolDaoSupport implements IAdminBasesDao {

    @Override
    public List<BaseAnneeScolaire> getAllBases() {
        
        List<BaseAnneeScolaire> bases = getJdbcTemplate().query("SELECT * FROM " 
                                                                        + TABLE_BASE + " b, " 
                                                                        + TABLE_ETABLISSEMENT + " e, "
                                                                        + TABLE_UTILISATEURS + " u "
                                                                        + "WHERE b.etablissementid=e.etablissementid "
                                                                        + " AND u.id=b.administrateurid ", new BaseAnneeScolaireRowMapper());
        
        return bases;
    }

    @Override
    public long saveBase(BaseAnneeScolaire base) {
        long baseId = 0;
        if(base.getId() == 0){
            getJdbcTemplate().update("INSERT INTO " + TABLE_BASE + " (annee,etablissementid,date_creation,debut,fin) VALUES(?,?,NOW(),?,?)", new Object[]{
               base.getAnnee(),
               base.getEtablissement().getId(),
               base.getCalendrier().getDebut(),
               base.getCalendrier().getFin()
            });
            
            baseId = getJdbcTemplate().queryForObject("SELECT LAST_INSERT_ID()", Long.class);
            base.setId(baseId);
            
        }else{
            getJdbcTemplate().update("UPDATE " + TABLE_BASE + " SET annee=?,etablissementid=?,administrateurid=?,debut=?, fin=? WHERE baseid=?", new Object[]{
               base.getAnnee(),
               base.getEtablissement().getId(),
               base.getAdministrateur().getId(),
               base.getCalendrier().getDebut(),
               base.getCalendrier().getFin(),
               base.getId()
            });
            
            baseId = base.getId();
        }
        
        return baseId;
    }

    @Override
    public BaseAnneeScolaire getBaseById(long baseId) {
        
        BaseAnneeScolaire base = (BaseAnneeScolaire) getJdbcTemplate().queryForObject("SELECT * FROM " + TABLE_BASE + " b, " 
            + TABLE_ETABLISSEMENT + " e, "
            + TABLE_UTILISATEURS + " u "
            + "WHERE b.etablissementid=e.etablissementid "
            + "AND b.baseid=? "
            + "AND u.id=b.administrateurid ", 
        new Object[]{
            baseId
        }, new BaseAnneeScolaireRowMapper());
        
        return base;
    }

    @Override
    public boolean deleteBase(BaseAnneeScolaire base) {
        
        /*int nbRowsAffected = getJdbcTemplate().update("DELETE FROM " + TABLE_BASE + " WHERE baseid=? ", new Object[]{
           base.getId()
        });
        
        return nbRowsAffected == 1;
            */
        
        int nbRowAffected = getJdbcTemplate().update("CALL SUPPRIMER_BASE(?)", new Object[]{
           base.getId()
        });
        
        return nbRowAffected == 1;
    }
            
}
